package com.cyan.note.mapper;

import com.cyan.note.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单管理Mapper接口
 * 
 * @author ZhangzhenYu
 * @since 2025-01-15
 */
@Mapper
public interface OrderMapper {

    /**
     * 分页查询订单列表
     */
    List<Order> selectOrderPage(
            @Param("shopId") Long shopId,
            @Param("customerName") String customerName,
            @Param("workerName") String workerName,
            @Param("orderType") String orderType,
            @Param("orderSource") String orderSource,
            @Param("status") List<String> status,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("sortBy") String sortBy,
            @Param("sortOrder") String sortOrder,
            @Param("offset") Integer offset,
            @Param("limit") Integer limit
    );

    /**
     * 查询订单总数
     */
    int countOrders(
            @Param("shopId") Long shopId,
            @Param("customerName") String customerName,
            @Param("workerName") String workerName,
            @Param("orderType") String orderType,
            @Param("orderSource") String orderSource,
            @Param("status") List<String> status,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime
    );

    /**
     * 根据ID查询订单
     */
    Order selectById(@Param("id") Long id);

    /**
     * 插入订单
     */
    int insert(Order order);

    /**
     * 更新订单
     */
    int updateById(Order order);

    /**
     * 删除订单
     */
    int deleteById(@Param("id") Long id);

    /**
     * 查询今日核心统计数据
     */
    TodayStatsResult selectTodayStats(@Param("shopId") Long shopId, @Param("today") LocalDateTime today);

    /**
     * 查询营业趋势数据
     */
    List<RevenueTrendItem> selectRevenueTrend(
            @Param("shopId") Long shopId,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate
    );

    /**
     * 查询进行中订单监控数据
     */
    List<ActiveOrderResult> selectActiveOrders(@Param("shopId") Long shopId);

    /**
     * 查询待分配订单数量
     */
    Integer countPendingOrders(@Param("shopId") Long shopId);

    /**
     * 查询超时未分配订单数量
     */
    Integer countOverdueOrders(@Param("shopId") Long shopId, @Param("timeoutMinutes") Integer timeoutMinutes);

    /**
     * 生成订单号
     */
    String generateOrderNo(@Param("shopId") Long shopId, @Param("date") String date);

    /**
     * 根据ID和店铺ID查询订单
     */
    Order selectByIdAndShop(@Param("id") Long id, @Param("shopId") Long shopId);

    /**
     * 根据条件查询订单列表
     */
    List<Order> selectByCondition(@Param("params") java.util.Map<String, Object> params);

    /**
     * 根据条件统计订单数量
     */
    Long countByCondition(@Param("params") java.util.Map<String, Object> params);

    /**
     * 查询订单状态统计
     */
    com.cyan.note.dto.manager.order.OrderStatusStatsDTO selectOrderStatusStats(@Param("params") java.util.Map<String, Object> params);

    /**
     * 今日统计结果
     */
    class TodayStatsResult {
        private Integer todayOrders;
        private Integer yesterdayOrders;
        private BigDecimal todayRevenue;
        private BigDecimal yesterdayRevenue;

        public Integer getTodayOrders() { return todayOrders; }
        public void setTodayOrders(Integer todayOrders) { this.todayOrders = todayOrders; }
        public Integer getYesterdayOrders() { return yesterdayOrders; }
        public void setYesterdayOrders(Integer yesterdayOrders) { this.yesterdayOrders = yesterdayOrders; }
        public BigDecimal getTodayRevenue() { return todayRevenue; }
        public void setTodayRevenue(BigDecimal todayRevenue) { this.todayRevenue = todayRevenue; }
        public BigDecimal getYesterdayRevenue() { return yesterdayRevenue; }
        public void setYesterdayRevenue(BigDecimal yesterdayRevenue) { this.yesterdayRevenue = yesterdayRevenue; }
    }

    /**
     * 营业趋势项
     */
    class RevenueTrendItem {
        private String date;
        private BigDecimal revenue;
        private Integer orders;

        public String getDate() { return date; }
        public void setDate(String date) { this.date = date; }
        public BigDecimal getRevenue() { return revenue; }
        public void setRevenue(BigDecimal revenue) { this.revenue = revenue; }
        public Integer getOrders() { return orders; }
        public void setOrders(Integer orders) { this.orders = orders; }
    }

    /**
     * 进行中订单结果
     */
    class ActiveOrderResult {
        private Long orderId;
        private String orderNo;
        private String serviceName;
        private String customerName;
        private String customerPhone;
        private String workerNames;
        private LocalDateTime startTime;
        private BigDecimal durationHours;
        private String status;

        public Long getOrderId() { return orderId; }
        public void setOrderId(Long orderId) { this.orderId = orderId; }
        public String getOrderNo() { return orderNo; }
        public void setOrderNo(String orderNo) { this.orderNo = orderNo; }
        public String getServiceName() { return serviceName; }
        public void setServiceName(String serviceName) { this.serviceName = serviceName; }
        public String getCustomerName() { return customerName; }
        public void setCustomerName(String customerName) { this.customerName = customerName; }
        public String getCustomerPhone() { return customerPhone; }
        public void setCustomerPhone(String customerPhone) { this.customerPhone = customerPhone; }
        public String getWorkerNames() { return workerNames; }
        public void setWorkerNames(String workerNames) { this.workerNames = workerNames; }
        public LocalDateTime getStartTime() { return startTime; }
        public void setStartTime(LocalDateTime startTime) { this.startTime = startTime; }
        public BigDecimal getDurationHours() { return durationHours; }
        public void setDurationHours(BigDecimal durationHours) { this.durationHours = durationHours; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
    }
}